Summary of Java's Method for Deleting Duplicate Elements in Sorted Array [Comparison of Three Methods]

  • 2021-06-29 11:01:43
  • OfStack

An example of how Java removes duplicate elements from a sorted array is given.Share it for your reference, as follows:

Title Description:

Given a sorted array, delete duplicate numbers from the original array so that each element appears only once, and return the length of the new array.

Do not use additional array space, it must be done without additional space in place.

1: Solve via ArrayList

Both time and space complexity are O (n)


ArrayList<Integer> list = new ArrayList<Integer>();
//  Remove duplicate elements from the array 
public int removeTheagain01(int[] array) {
    if (array == null || array.length == 0) {
      return 0;
    } else if (array.length == 1) {
      return 1;
    } else {
      int i = 0;
      int n = array.length - 1;
      while (i <= n) {
        if (i == n) {
          list.add(array[i]);
          i++;
        } else {
          int j = i + 1;
          if (array[i] == array[j]) {
            while (j <= n && array[i] == array[j]) {
              j++;
            }
          }
          list.add(array[i]);
          i = j;
        }
      }
      for (int k = 0; k < list.size(); k++) {
        array[k] = list.get(k);
      }
      return list.size();
    }
}

2: Use the System.arraycopy() function to copy arrays

The time complexity is O (n^2) and the space complexity is O (n)


public int removeTheagain02(int[] array) {
    if (array == null || array.length == 0) {
      return 0;
    } else if (array.length == 1) {
      return 1;
    } else {
      int end = array.length - 1;
      for (int i = 0; i <= end; i++) {
        if (i < end) {
          int j = i + 1;
          if (array[i] == array[j]) {
            while (j <= end && array[i] == array[j]) {
              j++;
            }
          }
          System.arraycopy(array, j, array, i + 1, end - j + 1);
          end -= j - i - 1;
        }
      }
      return end + 1;
    }
}

3: Solve problems with temporary variables

Time Complexity O (N), Spatial Complexity O (1)


public int removeTheagain03(int[] array) {
    if (array == null || array.length == 0) {
      return 0;
    } else if (array.length == 1) {
      return 1;
    } else {
      int temp = array[0];
      int len = 1;
      for (int i = 1; i < array.length; i++) {
        if (temp == array[i]) {
          continue;
        } else {
          temp = array[i];
          array[len] = array[i];
          len++;
        }
      }
      return len;
    }
}

Summary:

Array subscripts (pointers) and temporary variables are two great tools for solving array-related interview questions**

PS: There are two more simple and practical online text repetition tools recommended for everyone to use:

Online Duplicate Removal Tool:
http://tools.ofstack.com/code/quchong

Online Text Repetition Tool:
http://tools.ofstack.com/aideddesign/txt_quchong

More readers interested in java-related content can view this site's topics: Java Array Operation Skills Summary, Java Character and String Operation Skills Summary, Java Mathematical Operation Skills Summary, Java Data Structure and Algorithms Tutorial, and Java Operation DOM Node Skills Summary.

I hope that the description in this paper will be helpful to everyone's java program design.


Related articles: